// 自定义的redux状态管理

export const createStore = reducer => {

  let state = undefined
  let tasks = []

  // store.dispatch({type,payload})
  const dispath = action => {
    state = reducer(state, action)
    tasks.forEach(cb => cb())
  }

  const subscribe = cb => {
    tasks.push(cb)
    return () => tasks.splice(tasks.indexOf(cb), 1)
  }

  const getState = () => state

  // 初始化
  dispath({ type: '@@init@@' })

  return {
    dispath,
    subscribe,
    getState
  }

}
